#!/usr/bin/python3
# python-deps - find the dependencies of a given python script.
#
# This script is packaged into anaconda-dracut, installed, and run indirectly
# by lorax to create initramfs with all the python-related things we need.
# See also:  module-setup.sh
#
# Copyright (C) 2012-2021 by Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import copy
import os
import sys
import sysconfig
from importlib.util import find_spec
from modulefinder import ModuleFinder

# stringprep is needed by the idna encoding, and idna is needed by requests.
# The encoding import is implicit so ModuleFinder doesn't find it right.
alsoNeeded = {find_spec('requests').origin: find_spec('stringprep').origin}

# A couple helper functions...
def moduledir(pyfile):
    '''Given a python file, return the module dir it belongs to, or None.'''
    for topdir in sys.path:
        if topdir.startswith(sys.prefix + '/') and os.path.isdir(topdir):
            relpath = os.path.relpath(pyfile, topdir)
            if '/' not in relpath: continue
            modname = relpath.split('/')[0]
            if modname not in ('..', 'site-packages'):
                return os.path.join(topdir, modname)

# pylint: disable=redefined-outer-name
def pyfiles(moddir):
    '''basically, "find $moddir -type f -name "*.py"'''
    for curdir, _dirs, files in os.walk(moddir):
        for f in files:
            if f.endswith(".py"):
                yield os.path.join(curdir, f)

# OK. Use modulefinder to find all the modules etc. this script uses!
mods = []
deps = []

scripts = copy.copy(sys.argv[1:])
scripts.append(find_spec('site').origin)
scripts.append(find_spec('sysconfig').origin)
# platform-specific sysconfigdata module, needed by sysconfig, not
# detected by ModuleFinder - RHBZ #1409177, Python #29113
# this will only work and is only needed on Python 3.6+, so hedge it
try:
    sysconfmod = os.path.join(find_spec(sysconfig._get_sysconfigdata_name()).origin)
    if os.path.exists(sysconfmod):
        scripts.append(sysconfmod)
except AttributeError:
    # _get_sysconfigdata_name won't exist on older Pythons
    pass

while scripts:
    script = scripts.pop()

    if script == 'frozen':
        # https://docs.python.org/3.11/whatsnew/3.11.html#frozen-imports-static-code-objects
        continue

    finder = ModuleFinder()
    finder.run_script(script) # parse the script
    for mod in finder.modules.values():
        if not mod.__file__: # this module is builtin, so we can skip it
            continue

        if mod.__file__ not in deps: # grab the file itself
            deps.append(mod.__file__)

        moddir = moduledir(mod.__file__)  # if it's part of a module...
        if moddir and moddir not in mods: #
            deps += list(pyfiles(moddir)) # ...get the whole module
            mods.append(moddir)

        if mod.__file__ in alsoNeeded and alsoNeeded[mod.__file__] not in deps:
            scripts.append(alsoNeeded[mod.__file__])

# Include some bits that the python install itself needs
print(sysconfig.get_makefile_filename())
print(sysconfig.get_config_h_filename())

# And print the list of unique deps.
for d in set(deps):
    print(d)
